// Kony


#include "AbilitySystem/Passive/PassiveNiagaraComponent.h"

#include "AbilitySystemBlueprintLibrary.h"
#include "AbilitySystemComponent.h"
#include "BaseGameplayTags.h"
#include "AbilitySystem/BaseAbilitySystemComponent.h"
#include "Interaction/CombatInterface.h"

//构造函数
UPassiveNiagaraComponent::UPassiveNiagaraComponent()
{
	//不自动激活
	bAutoActivate = false;
}

void UPassiveNiagaraComponent::BeginPlay()
{
	Super::BeginPlay();
	//获取自定义能力系统组件
	if (UBaseAbilitySystemComponent* BaseASC = Cast<UBaseAbilitySystemComponent>(UAbilitySystemBlueprintLibrary::GetAbilitySystemComponent(GetOwner())))
	{
		// 绑定技能效果激活广播 回调函数
		BaseASC->ActivatePassiveEffectDelegate.AddUObject(this,&UPassiveNiagaraComponent::OnPassiveActivate);
		//激活已装备的被动能力
		ActivateIfEquipped(BaseASC);
	}
	else if (ICombatInterface* CombatInterface = Cast<ICombatInterface>(GetOwner()))
	{
		//如果上面能力系统组件没有成功获取，并存在战斗接口，从过战斗接口获取有效的能力系统组件
		CombatInterface->GetOnASCRegisteredDelegate().AddLambda([this](UAbilitySystemComponent* InASC)
		{
			if (UBaseAbilitySystemComponent* BaseASC = Cast<UBaseAbilitySystemComponent>(UAbilitySystemBlueprintLibrary::GetAbilitySystemComponent(GetOwner())))
			{
				BaseASC->ActivatePassiveEffectDelegate.AddUObject(this,&UPassiveNiagaraComponent::OnPassiveActivate);
				//激活已装备的被动能力
				ActivateIfEquipped(BaseASC);
			}
		});
	}
	
}
//接收激活广播回调函数
void UPassiveNiagaraComponent::OnPassiveActivate(const FGameplayTag& AbilityTag, bool bActivate)
{
	//判断标签匹配
	if (AbilityTag.MatchesTagExact(PassiveSpellTag))
	{
		//判断广播的是激活 并能力没有激活的情况下
		if (bActivate && !IsActive())
		{
			Activate();//激活
		}
		else
		{
			Deactivate();//停用
		}
	}
}
//激活已装备的被动能力
void UPassiveNiagaraComponent::ActivateIfEquipped(UBaseAbilitySystemComponent* BaseASC)
{
	//检查被动能力是否是装备状态
	const bool bStartupAbilitiesActivated = BaseASC->bStartupAbilitiesGiven;
	if (bStartupAbilitiesActivated)
	{
		//判断当前技能标签是否是装备状态，如果是则激活
		if (BaseASC->GetStatusTagFromAbilityTag(PassiveSpellTag) == FBaseGameplayTags::Get().Abilities_Status_Equipped)
		{
			Activate();
		}
	}
}
